package zpe.controledeacesso.model.bean;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import zpe.genericcrud.model.Entidade;

@Entity
@Table( name = "tb_transacao", schema = "dbo" )
public class Transacao implements Entidade{
	
	private static final long serialVersionUID = 3176814900832143343L;
	
	@Id
	@GeneratedValue( strategy=GenerationType.IDENTITY )
	@Column( name = "ci_transacao" )
	private Integer id;
	
	@Column( name = "nm_transacao" )
	private String descricao;
	
	@ManyToMany( fetch = FetchType.EAGER )
	@JoinTable( name = "dbo.tb_modulotransacao", joinColumns = @JoinColumn ( name = "cd_transacao" ) ,inverseJoinColumns = @JoinColumn( name = "cd_modulo" ) )
	private List<Modulo> modulos = new ArrayList<Modulo>();
	
	@Column( name = "nm_label" )
	private String label;
	
	@Transient
	private Modulo modulo = new Modulo();
	
	@Transient
	private boolean marcada = false;

	public Modulo getModulo() {
		return modulo;
	}

	public void setModulo(Modulo modulo) {
		this.modulo = modulo;
	}

	public Integer getId() {
		return id;
	}
	
	/**
	 * @return the descricao
	 */
	public String getDescricao() {
		return descricao;
	}


	/**
	 * @return the modulos
	 */
	public List<Modulo> getModulos() {
		return modulos;
	}

	/**
	 * @param modulos the modulos to set
	 */
	public void setModulos(List<Modulo> modulos) {
		this.modulos = modulos;
	}

	/**
	 * @param descricao the descricao to set
	 */
	public void setDescricao(String descricao) {
		if(descricao != null)
			descricao =  descricao.toUpperCase();
		this.descricao = descricao;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public boolean isEmpty() {
		return ( id == null && (descricao == null || descricao.trim().isEmpty())) ;
	}
	
	/**
	 * @return the marcada
	 */
	public boolean isMarcada() {
		return marcada;
	}

	/**
	 * @param marcada the marcada to set
	 */
	public void setMarcada(boolean marcada) {
		this.marcada = marcada;
	}
	
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * Label utilizada pela transação. Se o atributo label for nulo, passa o id como label. 
	 * @return
	 */
	public String getUsedLabel(){
		if( getLabel() != null ){
			return getLabel().trim();
		}else{
			return getId() == null ? null : getId().toString();
		}
	}
	
	public Map<String, Object> notEmptyFields() {
		Map<String, Object> map = new HashMap<String, Object>();
		if( descricao != null && !descricao.trim().isEmpty() )
			map.put("descricao", descricao);	
		return map;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Transacao other = (Transacao) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}	
}
